package com.open.party.cloud.view.examine

import android.os.Bundle
import android.view.View
import android.widget.RelativeLayout
import android.widget.TextView
import cn.android.x.model.data.ResultInfo
import cn.sinothk.hussars.parent.AppEventBusRecycleViewTitleBaseActivity
import com.jiangyy.easydialog.CommonDialog
import com.open.party.cloud.R
import com.open.party.cloud.model.ExamineBean
import com.open.party.cloud.model.ExamineClassEntity
import com.open.party.cloud.model.ExamineClassJoinVO
import com.open.party.cloud.model.data.LocalCache
import com.open.party.cloud.view.examine.adapter.ExamineClassesExamineListAdapter
import com.open.party.cloud.viewModel.ExamineViewModel
import com.sinothk.android.utils.XUtils
import com.sinothk.android.utils.inters.OnSuperListener
import com.sinothk.dialog.bottomDialog.shareDialog.BaseBottomDialog
import com.sinothk.rxretrofit.bean.PageVo
import kotlinx.android.synthetic.main.actvity_examine_classes_ex_list_view.*
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

class ExamineClassExamineListActivity : AppEventBusRecycleViewTitleBaseActivity<String>(), OnSuperListener<ExamineBean> {

    var viewModel: ExamineViewModel? = null
    var examineClass: ExamineClassEntity? = null

    private var adapter: ExamineClassesExamineListAdapter? = null

    private var thisActivity: ExamineClassExamineListActivity? = null

    override fun getLayoutResId(): Int = R.layout.actvity_examine_classes_ex_list_view

    private fun showBottomView() {
        ShareBottomDialog(this).show(supportFragmentManager)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        thisActivity = this
        examineClass = intent.getSerializableExtra("examineClass") as ExamineClassEntity
        if ("gbksb" == examineClass!!.classType) {
            setViewTitle(XUtils.string().getNotNullValue(examineClass!!.className, "班级试卷列表"))
            doAttendanceItem.visibility = View.GONE
        } else {
//            setViewTitle(XUtils.string().getNotNullValue(examineClass!!.className, "班级试卷列表"), R.drawable.icon_more_balck) {
//                showBottomView()
//            }
            setViewTitle(XUtils.string().getNotNullValue(examineClass!!.className, "班级试卷列表"))
            doAttendanceItem.visibility = View.VISIBLE
        }

        titleBarView.setCenterTxtColor(R.color.page_title)

        viewModel = ExamineViewModel()

        initView()
    }

    private fun initView() {
        initRecycleLinearView(loadingRecyclerView)

        adapter = ExamineClassesExamineListAdapter(this)
        loadingRecyclerView.adapter = adapter
        adapter?.setOnSuperListener(this)
        loadingRecyclerView.setLoadingMoreEnabled(false)

        loadingTipView.setRetryListener {
            refresh()
        }

        doAttendanceItem.setOnClickListener {
//            XUtils.intent().openActivity(this@ExamineClassExamineListActivity, ExamineClassDoAttendanceListActivity::class.java)
//                .putStringExtra("classId", examineClass?.id)
//                .start()
            showBottomView()
        }
        showBottomView()
    }

    override fun onResume() {
        super.onResume()
        refresh()
    }

    private fun refresh() {
        loadingTipView.showLoading("努力加载中")
        refreshData()
    }

    override fun loadData(pageVo: PageVo<String>) {
        viewModel!!.getWzExaminationList(XUtils.string().getNotNullValue(examineClass!!.id))
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public fun eventBusCallback(result: ResultInfo<List<ExamineBean>>) {
        if (result.eventType != "getWzExaminationList") {
            return
        }

        when (result.code) {
            ResultInfo.SUCCESS -> {
                if (result.data.isEmpty()) {
                    loadingTipView.showEmpty("暂无数据", R.drawable.empty_nodata)
                    loadingRecyclerView.setLoadingMoreEnabled(false)
                } else {
                    adapter!!.setData(result.data!!)
                    loadingTipView.showContent()
                }
            }
            ResultInfo.TOKEN_OVERDUE -> {
                loadingTipView.showError("Token过期，请重新登录")
            }
            else -> {
                loadingTipView.showError(result.msg)
            }
        }

        stopLoading(loadingRecyclerView, loadType)
    }

    override fun onClick(position: Int, itemData: ExamineBean, flag: String?) {

//        if ("考试中" == XUtils.string().getNotNullValue(itemData.examinationState)) {
//            CommonDialog.Builder(this)
//                .setCancelable(false)
//                .setTitle("考试提示")
//                .setMessage("进入考试后，需要提交答案后才能退出，中途关闭程序将判为0分，是否确定参加考试？")
//                .setPositiveButton("确定") {
//                    showLoadingDialog("进入考试")
//                    examineId = itemData.id
//
//                    val vo = ExamineClassJoinVO()
//                    vo.userId = LocalCache.getLoginUserId()
//                    vo.examinationId = examineId
//                    viewModel!!.insertStartExam(vo)
//                }.setNegativeButton("取消") {
//                }.show()
//        }else{
//            XUtils.toast().show(XUtils.string().getNotNullValue(itemData.examinationState))
//        }

        if (itemData.isTest() && itemData.isTime) {
            CommonDialog.Builder(this)
                .setCancelable(false)
                .setTitle("考试提示")
                .setMessage("进入考试后，需要提交答案后才能退出，中途关闭程序将判为0分，是否确定参加考试？")
                .setPositiveButton("确定") {
                    showLoadingDialog("进入考试")
                    examineId = itemData.id

                    val vo = ExamineClassJoinVO()
                    vo.userId = LocalCache.getLoginUserId()
                    vo.examinationId = examineId
                    viewModel!!.insertStartExam(vo)
                }.setNegativeButton("取消") {
                }.show()
//        } else if (itemData.isTest()) {
//            XUtils.toast().show("已考试")
        } else if (!itemData.isTime) {
            XUtils.toast().show("当前不是考试时间")
        } else {
            XUtils.toast().show(itemData.examinationState)
        }
    }

    var examineId = ""

    @Subscribe(threadMode = ThreadMode.MAIN)
    public fun insertStartExamCallback(result: ResultInfo<String>) {
        if (result.eventType != "insertStartExam") {
            return
        }

        hideLoadingDialog()

        when (result.code) {
            ResultInfo.SUCCESS -> {
                XUtils.intent().openActivity(this, ExamineDoingActivity::class.java)
                    .putStringExtra("examinationId", examineId)
                    .start()
            }
            ResultInfo.TOKEN_OVERDUE -> {
                loadingTipView.showError("Token过期，请重新登录")
            }
            else -> {
                loadingTipView.showError(result.msg)
            }
        }
    }

    class ShareBottomDialog(private val thisActivity: ExamineClassExamineListActivity) : BaseBottomDialog() {

        override fun getDialogLayoutRes(): Int {
            return R.layout.examine_class_dialog_layout
        }

        override fun bindDialogView(v: View) {
            val item01: RelativeLayout = v.findViewById(R.id.item01)
            val item02: RelativeLayout = v.findViewById(R.id.item02)
            val item03: RelativeLayout = v.findViewById(R.id.item03)
            val item05: RelativeLayout = v.findViewById(R.id.item05)

            val item10: RelativeLayout = v.findViewById(R.id.item10)
            val item12: RelativeLayout = v.findViewById(R.id.item12)
            val item13: RelativeLayout = v.findViewById(R.id.item13)
            val item15: RelativeLayout = v.findViewById(R.id.item15)

            val cancelItem: TextView = v.findViewById(R.id.cancelItem)

            item01.setOnClickListener {
                XUtils.intent().openActivity(thisActivity, ExamineClassMemberListActivity::class.java)
                    .putStringExtra("classId", thisActivity.examineClass!!.id)
                    .start()
                dismiss()
            }
            item02.setOnClickListener {
                XUtils.intent().openActivity(thisActivity, ExamineClassDoAttendanceListActivity::class.java)
                    .putStringExtra("classId", thisActivity.examineClass!!.id)
                    .start()
                dismiss()
            }
            item03.setOnClickListener {
                XUtils.intent().openActivity(thisActivity, ExamineClassTeacherRankListActivity::class.java)
                    .putStringExtra("classId", thisActivity.examineClass!!.id)
                    .start()
                dismiss()
            }
            item05.setOnClickListener {
                XUtils.intent().openActivity(thisActivity, ExamineClassCoursePageActivity::class.java)
                    .putStringExtra("classId", thisActivity.examineClass!!.id)
                    .start()
                dismiss()
            }

            cancelItem.setOnClickListener {
                dismiss()
            }

            item10.setOnClickListener {
                XUtils.intent().openActivity(thisActivity, ExamineClassTeachingRankActivity::class.java)
                    .putStringExtra("classId", thisActivity.examineClass!!.id)
                    .putStringExtra("className", thisActivity.examineClass!!.className)
                    .start()
                dismiss()
            }
            item12.setOnClickListener {
                XUtils.intent().openActivity(thisActivity, ExamineClassObjectsRankActivity::class.java)
                    .putStringExtra("classId", thisActivity.examineClass!!.id)
                    .putStringExtra("className", thisActivity.examineClass!!.className)
                    .start()
                dismiss()
            }

            item13.setOnClickListener {
                XUtils.intent().openActivity(thisActivity, ExamineClassHistoryMainActivity::class.java)
                    .putStringExtra("classId", thisActivity.examineClass!!.id)
                    .putStringExtra("className", thisActivity.examineClass!!.className)
                    .start()
                dismiss()
            }

            item15.setOnClickListener {
                XUtils.intent().openActivity(thisActivity, ExamineClassMemberCheckListActivity::class.java)
                    .putStringExtra("classId", thisActivity.examineClass!!.id)
                    .start()
                dismiss()
            }
        }
    }
}